package listen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import logging.LogWriter;

import users.GroupTask;
import users.TaskData;
import users.UserAccount;

public class AccountDatabase {
	private static int userAccountCounter = 100; // Assigns unique userID to each new user account.
	private static int groupTaskCounter = 100; // Assigns unique group task ID to each new group task.
	private HashMap<String, UserAccount> userList = new HashMap<String, UserAccount>(); // Holds valid users and account info.
	private HashMap<String, String> validUserAccounts = new HashMap<String, String>(); // Holds valid users and passwords.
	private ArrayList<GroupTask> group = new ArrayList<GroupTask>(); // Holds all group tasks.
	
	private final LogWriter serverLog = new LogWriter();

	public AccountDatabase() {
		loadDummyAccount();
	}
	
	public void loadDummyAccount() {
		UserAccount user = new UserAccount(userAccountCounter++, "DF", "pass");
		validUserAccounts.put(user.getUserName(), user.getPassword());
		user.addTask(new TaskData("DF\n21\n-1\nHomework\nHome\n200\n-200\nfalse\ntrue\n<body>Update project log.\nAdd new menu.<body><taskbreak>\n"));
		user.addTask(new TaskData("DF\n22\n-1\nOil Change\nMechanic\n300\n-300\nfalse\ntrue\n<body>Use coupon for oil change.<body><taskbreak>"));
		user.addTask(new TaskData("DF\n23\n-1\nMovie\nTheater\n400\n-400\nfalse\ntrue\n<body>Pay for movie tickets.<body><taskbreak>"));
		userList.put(user.getUserName(), user);
		
		user = new UserAccount(userAccountCounter++, "tester", "pass");
		validUserAccounts.put(user.getUserName(), user.getPassword());
		userList.put(user.getUserName(), user);
		
		user = new UserAccount(userAccountCounter++, "Kate", "pass");
		validUserAccounts.put(user.getUserName(), user.getPassword());
		userList.put(user.getUserName(), user);
		
		user = new UserAccount(userAccountCounter++, "Dennis", "pass");
		validUserAccounts.put(user.getUserName(), user.getPassword());
		userList.put(user.getUserName(), user);
				
		GroupTask newGroupTask = new GroupTask(groupTaskCounter++, new TaskData("Group\n-1\n-1\nGroceries\nSafeway\n800\n-800\nfalse\ntrue\n<body>paper towels\ntoothpaste\ncarrots<body><taskbreak>"));
		newGroupTask.addMember("DF");
		newGroupTask.addMember("tester");
		group.add(newGroupTask);
		newGroupTask = new GroupTask(groupTaskCounter++, new TaskData("Group\n-1\n-1\nTeam Meeting\nWork\n900\n-900\nfalse\ntrue\n<body>Personnel problems\nraises\nnew coffee maker<body><taskbreak>"));
		newGroupTask.addMember("tester");
		group.add(newGroupTask);
	}
	
	public int getAccountCounter() {
		return userAccountCounter;
	}
	
	public HashMap<String, String> getValidUserAccounts() {
		return validUserAccounts;
	}
	
	public ArrayList<GroupTask> getGroup() {
		return group;
	}
	
	public HashMap<String, UserAccount> getUserList() {
		return userList;
	}
	
	public void addUserAccount(String userName, String password) {
		UserAccount user = new UserAccount(userAccountCounter++,	userName, password);
		validUserAccounts.put(user.getUserName(), user.getPassword());
		userList.put(user.getUserName(), user);
		serverLog.log("New User added: " + userName);
	}
	
	public void createGroup(String user) {
		GroupTask newGroupTask = new GroupTask(groupTaskCounter++, new TaskData("Group\n-1\n-1\n\n\n0\n0\nfalse\ntrue\n<body><body><taskbreak>"));
		newGroupTask.addMember(user);
		group.add(newGroupTask);
	}
}
